/*     */ package com.newcrm.webapi.sub;
/*     */ 
/*     */ import com.newcrm.bean.CompanyInfo;
/*     */ import com.newcrm.bean.ExtInfo;
/*     */ import com.newcrm.bean.User;
/*     */ import com.newcrm.db.CustomerManager;
/*     */ import com.newcrm.exception.NotFoundException;
/*     */ import com.newcrm.util.JsoupManager;
/*     */ import com.newcrm.util.Util;
/*     */ import com.newcrm.webapi.AbstractAction;
/*     */ import com.newcrm.webapi.AbstractHttpServlet;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintWriter;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ import javax.servlet.ServletException;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ import net.sf.json.JSONObject;
/*     */ import org.apache.log4j.Logger;
/*     */ import org.jsoup.nodes.Document;
/*     */ 
/*     */ public class QisoukeSaveCustomer extends AbstractAction
/*     */ {
/*  37 */   private static Logger logger = Logger.getLogger(QisoukeSaveCustomer.class
/*  38 */     .getName());
/*     */ 
/*     */   public QisoukeSaveCustomer(AbstractHttpServlet servlet)
/*     */   {
/*  41 */     super(servlet);
/*     */   }
/*     */ 
/*     */   public void parse(HttpServletRequest request, HttpServletResponse response)
/*     */     throws IOException, ServletException, NotFoundException
/*     */   {
/*  48 */     User user = super.getLoginUser(request);
/*     */ 
/*  51 */     String companyName = null;
/*     */ 
/*  53 */     String linkMan = null;
/*  54 */     String duty = null;
/*     */ 
/*  57 */     String mobile = null;
/*  58 */     String tel = null;
/*  59 */     String fax = null;
/*     */ 
/*  61 */     String email = null;
/*  62 */     String qq = null;
/*  63 */     String homepage = null;
/*  64 */     String address = null;
/*  65 */     String reMark = null;
/*     */ 
/*  68 */     String permissionsId = request.getParameter("permissionsId");
/*     */ 
/*  72 */     int changeVisible = 
/*  73 */       Util.str2Int(request.getParameter("changeVisible"));
/*     */ 
/*  76 */     String id = Util.getURLParameter(request.getParameter("id"));
/*     */ 
/*  79 */     if ((id == null) || (id.trim().length() == 0))
/*     */     {
/*  81 */       companyName = request.getParameter("companyName");
/*     */ 
/*  84 */       linkMan = request.getParameter("linkMan");
/*  85 */       duty = request.getParameter("duty");
/*     */ 
/*  88 */       mobile = request.getParameter("mobile");
/*  89 */       tel = request.getParameter("tel");
/*  90 */       fax = request.getParameter("fax");
/*     */ 
/*  92 */       email = request.getParameter("email");
/*  93 */       qq = request.getParameter("qq");
/*  94 */       homepage = request.getParameter("homepage");
/*  95 */       address = request.getParameter("address");
/*  96 */       reMark = request.getParameter("reMark");
/*     */     } else {
/*  98 */       String searchUrl = "http://so.qikebao.com/qisouke/" + id;
/*  99 */       CompanyInfo company = super.getCompany(user.companyId);
/*     */ 
/* 101 */       JSONObject json = null;
/*     */       try {
/* 103 */         Document doc = JsoupManager.getSearchResult(user, company, 
/* 104 */           searchUrl.toString());
/* 105 */         JSONObject jsonObject = JSONObject.fromObject(doc.text());
/* 106 */         if (jsonObject != null)
/* 107 */           json = jsonObject.getJSONObject("item");
/*     */       }
/*     */       catch (Exception e)
/*     */       {
/*     */         try {
/* 112 */           Thread.sleep(1000L);
/*     */         } catch (InterruptedException localInterruptedException) {
/*     */         }
/*     */         try {
/* 116 */           Document doc = JsoupManager.getSearchResult(user, company, 
/* 117 */             searchUrl.toString());
/* 118 */           JSONObject jsonObject = JSONObject.fromObject(doc.text());
/* 119 */           if (jsonObject != null)
/* 120 */             json = jsonObject.getJSONObject("item");
/*     */         }
/*     */         catch (Exception e1) {
/* 123 */           logger.warn("search company detail info failed", e1);
/* 124 */           super.showErrorMessage(response, "搜索超时，请稍后再试");
/* 125 */           return;
/*     */         }
/*     */ 
/*     */       }
/*     */ 
/* 130 */       companyName = Util.getJSONStringValue(json, "CompanyName");
/*     */ 
/* 132 */       linkMan = Util.getJSONStringValue(json, "LinkMan");
/* 133 */       duty = Util.getJSONStringValue(json, "Duty");
/*     */ 
/* 136 */       mobile = Util.getJSONStringValue(json, "Mobile");
/* 137 */       tel = Util.getJSONStringValue(json, "Tel");
/* 138 */       fax = Util.getJSONStringValue(json, "Fax");
/*     */ 
/* 140 */       email = Util.getJSONStringValue(json, "Email");
/* 141 */       qq = Util.getJSONStringValue(json, "QQ");
/* 142 */       homepage = Util.getJSONStringValue(json, "Homepage");
/* 143 */       address = Util.getJSONStringValue(json, "Address");
/* 144 */       reMark = Util.getJSONStringValue(json, "ReMark");
/*     */     }
/*     */ 
/* 148 */     if (((companyName == null) || (companyName.trim().length() == 0)) && (
/* 149 */       (linkMan == null) || (linkMan.trim().length() == 0))) {
/* 150 */       logger.warn("company and linkman is null");
/* 151 */       super.showErrorMessage(response, "数据格式错误，没有公司/联系人");
/* 152 */       return;
/*     */     }
/*     */ 
/* 155 */     List companyExtInfos = new ArrayList();
/* 156 */     List peopleExtInfos = new ArrayList();
/*     */ 
/* 159 */     ExtInfo telExtInfo = new ExtInfo();
/* 160 */     telExtInfo.type = 1;
/* 161 */     telExtInfo.info1 = tel;
/* 162 */     companyExtInfos.add(telExtInfo);
/*     */ 
/* 164 */     ExtInfo faxExtInfo = new ExtInfo();
/* 165 */     faxExtInfo.type = 1;
/* 166 */     faxExtInfo.info1 = fax;
/* 167 */     faxExtInfo.info2 = "传真";
/* 168 */     companyExtInfos.add(faxExtInfo);
/* 169 */     peopleExtInfos.add(faxExtInfo);
/*     */ 
/* 171 */     ExtInfo homepageExtInfo = new ExtInfo();
/* 172 */     homepageExtInfo.type = 4;
/* 173 */     homepageExtInfo.info1 = homepage;
/* 174 */     companyExtInfos.add(homepageExtInfo);
/*     */ 
/* 176 */     ExtInfo addressExtInfo = new ExtInfo();
/* 177 */     addressExtInfo.type = 6;
/* 178 */     addressExtInfo.info1 = address;
/* 179 */     companyExtInfos.add(addressExtInfo);
/*     */ 
/* 182 */     ExtInfo mobileExtInfo = new ExtInfo();
/* 183 */     mobileExtInfo.type = 1;
/* 184 */     mobileExtInfo.info1 = mobile;
/* 185 */     peopleExtInfos.add(mobileExtInfo);
/*     */ 
/* 187 */     ExtInfo qqExtInfo = new ExtInfo();
/* 188 */     qqExtInfo.type = 3;
/* 189 */     qqExtInfo.info1 = qq;
/* 190 */     peopleExtInfos.add(qqExtInfo);
/*     */ 
/* 192 */     ExtInfo emailExtInfo = new ExtInfo();
/* 193 */     emailExtInfo.type = 2;
/* 194 */     emailExtInfo.info1 = email;
/* 195 */     peopleExtInfos.add(emailExtInfo);
/*     */ 
/* 198 */     CustomerManager manager = new CustomerManager();
/* 199 */     int companyId = manager.addCompany(companyName, 0, reMark, 
/* 200 */       changeVisible, permissionsId, null, null, null, 
/* 201 */       companyExtInfos, user.userId, user.companyId);
/*     */ 
/* 204 */     if (companyId < 0)
/*     */     {
/* 206 */       int peopleId = manager.addPeople(companyId * -1, linkMan, duty, 
/* 207 */         0, "", changeVisible, permissionsId, null, null, null, 
/* 208 */         peopleExtInfos, user.userId, user.companyId);
/*     */ 
/* 210 */       if (peopleId < 0) {
/* 211 */         super.showErrorMessage(response, "该公司和联系人在系统中已经存在，无法重复添加");
/* 212 */         return;
/*     */       }
/*     */ 
/* 215 */       super.showErrorMessage(response, "该公司已存在，现已将联系人信息补齐");
/* 216 */       return;
/*     */     }
/*     */ 
/* 220 */     manager.addPeople(companyId, linkMan, duty, 0, "", changeVisible, 
/* 221 */       permissionsId, null, null, null, peopleExtInfos, 
/* 222 */       user.userId, user.companyId);
/*     */ 
/* 224 */     response.setContentType("text/html;charset=utf-8");
/* 225 */     PrintWriter out = response.getWriter();
/* 226 */     JSONObject jsonObject = new JSONObject();
/* 227 */     jsonObject.put("error", "0");
/* 228 */     jsonObject.put("customerId", Integer.valueOf(companyId));
/* 229 */     out.println(jsonObject);
/* 230 */     out.flush();
/* 231 */     out.close();
/*     */   }
/*     */ }

/* Location:           C:\E\referenceProject\CRM参考\qikebao\WEB-INF\lib\qikebao.jar
 * Qualified Name:     com.newcrm.webapi.sub.QisoukeSaveCustomer
 * JD-Core Version:    0.6.1
 */